package
{
	import com.command.MouseCommand;
	import com.command.MouseCommandCreator;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.GroupSpecifier;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	import org.devboy.hydra.HydraChannel;
	import org.devboy.hydra.HydraEvent;
	import org.devboy.hydra.HydraService;
	import org.devboy.hydra.chat.ChatMessageCommand;
	import org.devboy.hydra.chat.ChatMessageCommandCreator;
	import org.devboy.hydra.commands.HydraCommandEvent;

	[SWF(width="640", height="480", frameRate=24, backgroundColor="#38226C")]
	public class ZonePk extends Sprite
	{
		private var _bg:Sprite;
		private var _statusBar:TextField;
		private var _uid:TextField;
		private var _login:TextField;
		private var _chat:TextField;
		private var _send:TextField;
		
		private var _hydraService:HydraService;
		private var _channel:HydraChannel;
		
		private var cursor:MyCursor;
		
		public function ZonePk()
		{
			init();
		}
		
		private function init():void{
			_bg = new Sprite();
			_bg.graphics.beginFill(0xffffff);
			_bg.graphics.drawRect(0,0,640,480);
			_bg.graphics.endFill();
			addChild(_bg);
			
			_statusBar = new TextField();
			_statusBar.border = true;
			_statusBar.x = 0;_statusBar.y = 430;
			_statusBar.width = 640;_statusBar.height = 50;
			_statusBar.text = '等待中...';
			addChild(_statusBar);
			
			_uid = new TextField();
			_uid.border = true;
			_uid.x = 10;_uid.y = 10;
			_uid.width = 60;_uid.height = 25;
			_uid.text = Math.floor(Math.random()*100).toString();
			_uid.type = TextFieldType.INPUT;
			addChild(_uid);
			
			_login = new TextField();
			_login.border = true;
			_login.x = 100;_login.y = 10;
			_login.width = 60;_login.height = 25;
			_login.backgroundColor = 0xff00ff;
			_login.text = '登陆...';
			_login.addEventListener(MouseEvent.CLICK,onLogin);
			addChild(_login);
			
			_chat = new TextField();
			_chat.border = true;
			_chat.x = 200;_chat.y = 10;
			_chat.width = 60;_chat.height = 25;
			_chat.text = '';
			_chat.type = TextFieldType.INPUT;
			addChild(_chat);
			
			_send = new TextField();
			_send.border = true;
			_send.x = 300;_send.y = 10;
			_send.width = 60;_send.height = 25;
			_send.backgroundColor = 0xff00ff;
			_send.text = '发送...';
			_send.addEventListener(MouseEvent.CLICK,onSend);
			addChild(_send);
			
			cursor = new MyCursor();
			addChild(cursor);
			
			var stratusServiceUrl : String = "rtmfp://stratus.rtmfp.net/9d90de53c55850ec38e8b5f8-daa6d72540f4";
			_hydraService = new HydraService("HydraChatExample", stratusServiceUrl);
			_hydraService.addEventListener(HydraEvent.SERVICE_CONNECT_SUCCESS, serviceEvent);
			_hydraService.addEventListener(HydraEvent.SERVICE_CONNECT_FAILED, serviceEvent);
			_hydraService.addEventListener(HydraEvent.SERVICE_CONNECT_REJECTED, serviceEvent);
		}
		
		private function onLogin(e:MouseEvent):void{
			var groupSpecifier : GroupSpecifier = new GroupSpecifier("HydraClickExample/ClickChannel");
			groupSpecifier.postingEnabled = true;
			groupSpecifier.serverChannelEnabled = true;
			_channel = new HydraChannel(_hydraService, "ZonePK/testroom1", groupSpecifier, false );
			_channel.addEventListener(HydraCommandEvent.COMMAND_RECEIVED, handleCommand);
			_hydraService.commandFactory.addCommandCreator(new MouseCommandCreator());
			_hydraService.commandFactory.addCommandCreator(new ChatMessageCommandCreator());
			_hydraService.connect(_uid.text);
			
		}
		
		private function handleCommand(event : HydraCommandEvent) : void
		{
			switch( event.command.type )
			{
				case MouseCommand.TYPE:
					handleRemoteMouse( event.command as MouseCommand );
					break;
				case ChatMessageCommand.TYPE:
					handleChatMessage(event.command as ChatMessageCommand);
					break;	
			}
		}
		private function onMouseMove(e:MouseEvent):void{
			_channel.sendCommand( new MouseCommand(mouseX, mouseY) );
		}
		private function handleRemoteMouse(clickCommand : MouseCommand) : void
		{
			cursor.destX = clickCommand.x;
			cursor.destY = clickCommand.y;
		}
		public function sendChatMessage( chatMessage : String ) : void
		{
			_channel.sendCommand(new ChatMessageCommand(chatMessage));
		}
		
		private function handleChatMessage(command : ChatMessageCommand) : void
		{
			_statusBar.text = command.userId + ':' + command.chatMessage;
		}
		private function onSend(e:MouseEvent):void{
			var say:String = _chat.text;
			_chat.text = '';
			sendChatMessage(say);
			_statusBar.text = '信息已发送';
		}
		
		private function serviceEvent(event : HydraEvent) : void
		{
			switch( event.type )
			{
				case HydraEvent.SERVICE_CONNECT_SUCCESS:
					_statusBar.text = "Connected as " + _hydraService.user.name;
					addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
					break;
				case HydraEvent.SERVICE_CONNECT_FAILED:
					_statusBar.text = "Connection failed.";
					break;
				case HydraEvent.SERVICE_CONNECT_REJECTED:
					_statusBar.text = "Connection rejected.";
					break;
			}
		}
	}
}